﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib.USB.HID;
using WFNetLib;
using System.Windows.Forms;
using System.Diagnostics;

namespace 生命体征测试仪
{
    public partial class formMedical : formBase
    {
        IntPtr m_hUsbEventHandle;
        zfyUSB zfyHID = null;
        private void tabZhiFang_Enter(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            ZhiFang_Start.Enabled = false;
            ZhiFang_Result.Text = "";
            m_hUsbEventHandle = Win32Usb.RegisterForUsbEvents(Handle, Win32Usb.HIDGuid);
            if (zfyHID == null)
            {
                zfyHID = (zfyUSB)HIDDevice.FindDevice(0x10c4, 0x82cd, typeof(zfyUSB));
            }
            if (zfyHID != null)
            {
                if (zfyHID.CheckDevice())
                {
                    ZhiFang_Start.Enabled = true;
                }
                else
                {
                    zfyHID.Dispose();
                    zfyHID = null;
                }
            }       
        }

        private void tabZhiFang_Leave(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            Win32Usb.UnregisterForUsbEvents(m_hUsbEventHandle);
            if (zfyHID != null)
            {
                zfyHID.Dispose();
                zfyHID = null;
            }
        }
        private void ZhiFang_Start_Click(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            ZhiFang_Result.Text = "请握紧脂肪仪并等待10秒左右!";
            WFNetLib.WFGlobal.WaitMS(1);
            if (zfyHID.GetMeasure(170, 700, 36, 1))
            {
                MedicalResult.ZhiFang = (float)(BytesOP.MakeShort(zfyHID.zyfResult[1], zfyHID.zyfResult[0])) / 10;
                MedicalResult.BMI = (float)(BytesOP.MakeShort(zfyHID.zyfResult[2], zfyHID.zyfResult[3])) / 10;
                MedicalResult.XinChenDaiXie = BytesOP.MakeShort(zfyHID.zyfResult[4], zfyHID.zyfResult[5]);
                MedicalResult.TiZhi = zfyHID.zyfResult[6];
                MedicalResult.TiXing = zfyHID.zyfResult[7];
                bChange = true;
                ZhiFang_Result.Text = "测试完成!";
                ZhiFang_Result.Text += "脂肪:" + MedicalResult.ZhiFang.ToString("F1") + "%,";
                ZhiFang_Result.Text += "BMI:" + MedicalResult.BMI.ToString("F1") + ",";
                ZhiFang_Result.Text += "新陈代谢:" + MedicalResult.XinChenDaiXie.ToString() + "Kcal,";
                ZhiFang_Result.Text += "体质:" + ((Common.ZhiFang_TiZhi)zfyHID.zyfResult[6]).ToString() + ",";
                ZhiFang_Result.Text += "体形:" + ((Common.ZhiFang_TiXing)zfyHID.zyfResult[7]).ToString();
            }
            else
            {
                ZhiFang_Result.Text = zfyHID.strErr;
            }
        } 
        protected override void WndProc(ref Message m)
        {
            try
            {
                if (m.Msg == 0x0219)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case 0x8000:
                            if (zfyHID == null)
                            {
                                zfyHID = (zfyUSB)HIDDevice.FindDevice(0x10c4, 0x82cd, typeof(zfyUSB));
                                if (zfyHID != null)
                                {
                                    if (zfyHID.CheckDevice())
                                    {
                                        ZhiFang_Start.Enabled = true;
                                    }
                                    else
                                    {
                                        ZhiFang_Start.Enabled = false;
                                        zfyHID.Dispose();
                                        zfyHID = null;
                                    }
                                }
                            }
                            break;
                        case 0x8004:
                            try
                            {
                                ZhiFang_Start.Enabled = false;
                                zfyHID.Dispose();
                                zfyHID = null;
                            }
                            catch { }

                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.WndProc(ref m);
        }
    }    
    public class zfyData
    {
        public float zf;
        public float BMI;
        public ushort xcdx;
        public Common.ZhiFang_TiZhi tz;
        public Common.ZhiFang_TiXing tx;
    }
    public class zfyUSB : HIDDevice
    {
        public string strErr;
        public byte[] zyfResult = new byte[8];
        public bool CheckDevice()
        {
            InteractionData tx = new InteractionData(0x33);
            byte[] rxb = DeviceInteraction(tx.GetDataList());
            rxb = DeviceInteraction(tx.GetDataList());
            if (rxb[1] == 0xaa && rxb[2] == 0xaa && rxb[5] == 0x55 && rxb[6] == 0x55)
            {
                if (rxb[3] == 0xff && rxb[4] == 0x33)
                    return true;
            }
            return false;
        }
        public bool GetMeasure(byte sg, ushort tz, byte age, byte xb)
        {
            InteractionData tx = new InteractionData(0x77);
            tx.buff[0] = 0;
            tx.buff[1] = sg;
            tx.buff[2] = BytesOP.GetLowByte(tz);
            tx.buff[3] = BytesOP.GetHighByte(tz);
            tx.buff[4] = age;
            tx.buff[5] = xb;
            tx.buffLen = 6;
            byte[] rxb = DeviceInteraction(tx.GetDataList(), 20000, 1);
            if (rxb[3] == 0x00)
            {
                switch (rxb[5])
                {
                    case 0x01:
                        strErr = "设备未连接。";
                        return false;
                    case 0x02:
                        strErr = "设备准备开始测量，但是测量者长时间没有手握设备。";
                        return false;
                    case 0x03:
                        strErr = "手握姿势不正确或者测量时手有晃动。";
                        return false;
                    case 0x04:
                        strErr = "参数不正确或者超过设备支持的范围。";
                        return false;
                    case 0x05:
                        strErr = "测量结果超过系统支持范围。";
                        return false;
                    case 0x00:
                        strErr = "未知原因。";
                        return false;
                }
            }
            rxb = DeviceReceive(20000, 1);
            if (rxb[3] == 0x00)
            {
                switch (rxb[5])
                {
                    case 0x01:
                        strErr = "设备未连接。";
                        return false;
                    case 0x02:
                        strErr = "设备准备开始测量，但是测量者长时间没有手握设备。";
                        return false;
                    case 0x03:
                        strErr = "手握姿势不正确或者测量时手有晃动。";
                        return false;
                    case 0x04:
                        strErr = "参数不正确或者超过设备支持的范围。";
                        return false;
                    case 0x05:
                        strErr = "测量结果超过系统支持范围。";
                        return false;
                    case 0x00:
                        strErr = "未知原因。";
                        return false;
                }
            }
            if (rxb[3] == 0x77)
            {
                for (int i = 0; i < 8; i++)
                {
                    zyfResult[i] = rxb[4 + i];
                }
            }
            return true;
        }
    }
    public class InteractionData
    {
        public byte codeID;
        public byte[] buff = new byte[64];
        public int buffLen;
        public InteractionData()
        {
            buffLen = 0;
        }
        public InteractionData(byte _codeID)
        {
            codeID = _codeID;
            buffLen = 0;
        }
        public byte[] GetDataList()
        {
            byte[] list = new byte[65];
            list[0] = 0x00;
            list[1] = 0xaa;
            list[2] = 0xaa;
            list[3] = codeID;
            if (buffLen == 0)
            {
                list[4] = 0;
                list[5] = 0x55;
                list[6] = 0x55;
            }
            else
            {
                for (int i = 0; i < buffLen; i++)
                {
                    list[i + 4] = buff[i];
                }
                list[4 + buffLen] = 0x55;
                list[4 + buffLen + 1] = 0x55;
            }
            return list;
        }
    }
}
